package com.zhang;


import java.util.concurrent.TimeUnit;

public class Semaphore {
    public static void main(String[] args) {
        java.util.concurrent.Semaphore semaphore = new java.util.concurrent.Semaphore(3);
        for (int i = 0; i < 6; i++) {
            new Thread(()->{
                try {
                    semaphore.acquire();
                    System.out.println("线程："+Thread.currentThread().getName()+"\t抢到车位！");
                    TimeUnit.SECONDS.sleep(3);
                    System.out.println("线程："+Thread.currentThread().getName()+"\t占用3秒后离开车位！");
                }catch (Exception e){
                    e.printStackTrace();
                }finally {
                    semaphore.release();
                }
            },String.valueOf(i)).start();
        }
    }
}
